using System;
using System.Collections.Generic;
using System.Xml.Linq;
using Facebook;

namespace Facebook.Api
{
    /// <summary>Encapsulates information about an authenticated user session within a Facebook application.</summary>
    [Serializable]
    [FacebookObjectAttribute("session_info")]
    public class SessionInfo : FacebookObjectBase, ISessionInfo
    {
        /// <summary>Initializes a new instance of <see cref="SessionInfo" /> with the specified content.</summary>
        /// <param name="content">An <see cref="XElement" /> object containing xml data for the object.</param>
        public SessionInfo(XElement content) : base(content) { }

        /// <summary>Intializes an instance of <see cref="SessionInfo" />, prefilling the internal dictionary with the specified values.</summary>
        /// <param name="dict">An <see cref="IDictionary{String, Object}" /> object containing property names and values.</param>
        public SessionInfo(IDictionary<String, Object> dict) : base(dict) { }

        /// <summary>Intializes an instance of <see cref="SessionInfo" />.</summary>
        public SessionInfo() { }

        public virtual void Init(ISessionInfo sessionInfo)
        {
            this.BaseDomain = sessionInfo.BaseDomain;
            this.Expires = sessionInfo.Expires;
            this.Secret = sessionInfo.Secret;
            this.SessionKey = sessionInfo.SessionKey;
            this.Uid = sessionInfo.Uid;
        }

        /// <summary>Gets or sets the session key generated by the Facebook API for the current session.</summary>
        public String SessionKey
        {
            get { return this.GetString("session_key"); }
            set { this.InnerDictionary["session_key"] = value; }
        }
        
        /// <summary>Gets or sets the id of the Facebook user.</summary>
        public Int64 Uid
        {
            get { return this.GetValueType<Int64>("uid"); }
            set { this.InnerDictionary["uid"] = value; }
        }

        /// <summary>Gets or sets a <see cref="DateTime" /> value representing when the session expires.</summary>
        public DateTime Expires
        {
            get
            {
                if (this.InnerDictionary.ContainsKey("expires")) return (DateTime)this.InnerDictionary["expires"];
                else return UnixDateTime.ToDateTime(this.GetValueType<Int64>("expires"));
            }
            set { this.InnerDictionary["expires"] = value; }
        }

        /// <summary>Gets or sets the session secret used for communicating with the API with JavaScript or Flash.</summary>
        /// <remarks>See Facebook's documentation on <a href="http://wiki.developers.facebook.com/index.php/Session_Secret">Session Secret</a>.</remarks>
        public String Secret
        {
            get{ return this.GetString("secret");}
            set { this.InnerDictionary["secret"] = value; }
        }

        /// <summary>Gets or sets the base domain for web applications when the application is run on multiple domains.</summary>
        /// <remarks>See Facebook's documentation on <a href="http://wiki.developers.facebook.com/index.php/Base_Domain">Base Domain</a>.</remarks>
        public String BaseDomain
        {
            get { return this.GetString("base_domain"); }
            set { this.InnerDictionary["base_domain"] = value; }
        }
    }
}